﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.ContractManage
{
    public partial class ContractProfitPut : BasePage
    {
        public Guid ContractID
        { 
         get{
             if (txtContractID.Value != "")
             {
                 return new Guid(txtContractID.Value);
             }
             return Guid.Empty;
         }
            set {
                txtContractID.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    ContractID = new Guid(Request.QueryString["ContractID"]);
                    ReLoadData();
                }
            }
        }

        private void ReLoadData()
        {
            ContractCalculateBll bll = new ContractCalculateBll();
            CtrtContractEntity ConEntity = bll.Get<CtrtContractEntity>(ContractID);

            //合同商品成本
            decimal ContractItemCost = bll.SumContractItemCost(ContractID);
            ContractItemCost = decimal.Round(ContractItemCost, 2);
            lblItemCost.Text = ContractItemCost.ToString("N");

            //配套商品成本
            decimal GiftCost = 0;
            GiftCost = bll.GetContractGiftCost(new Guid(Request.QueryString["ContractID"]));
            txtPresentCost.Text = GiftCost.ToString("N");
 
            //资金成本 
            decimal CapitalCost = 0;
            CapitalCost = ConEntity.CapitalCost;
            radtxtCapitalCost.Value = (double)CapitalCost;

            //其他成本
            decimal OtherCost = 0;
            OtherCost = ConEntity.OtherCost;
            radtxtOtherCost.Value = (double)OtherCost;

            //销售配套试剂利润
            decimal TotalPrice = 0;
            TotalPrice = bll.SumContractPutItemPrice(ContractID);
            TotalPrice = TotalPrice * ConEntity.ContractProfitRate;
            lblContractTotalPrice.Text = TotalPrice.ToString("N");

            //其他收入
            decimal OthorMachineIncome = 0, OthorMaintainIncome = 0, OthorRentIncome = 0;
            CtrtContractOthorIncomeExtendEntity entity = new CtrtContractOthorIncomeExtendBll().Get<CtrtContractOthorIncomeExtendEntity>(ContractID);
            if (entity != null)
            {
                OthorMachineIncome = entity.MachineTotalIncome;
                OthorMaintainIncome = entity.MaintainTotalIncome;
                OthorRentIncome = entity.RentTotalIncome;
            }

           //其他收入 - 设备收入
            lblOthorMachineIncome.Text = OthorMachineIncome.ToString("N");
            //其他收入 - 保修收入
            lblOthorMaintainIncome.Text = OthorMaintainIncome.ToString("N");
            //其他收入 - 租赁收 入
            lblOthorRentIncome.Text = OthorRentIncome.ToString("N");

            //毛利润
            lblProfit.Text = (TotalPrice + OthorMachineIncome + OthorMaintainIncome + OthorRentIncome - ContractItemCost - GiftCost -  CapitalCost - OtherCost).ToString("N");
            if ((TotalPrice + OthorMachineIncome + OthorMaintainIncome + OthorRentIncome) != 0)
            {
                lblProfitRate.Text = ((TotalPrice + OthorMachineIncome + OthorMaintainIncome + OthorRentIncome - ContractItemCost - GiftCost - CapitalCost - OtherCost) * 100 / (TotalPrice + OthorMachineIncome + OthorMaintainIncome + OthorRentIncome)).ToString("N") + "%";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            decimal CapitalCost = 0,OtherCost = 0;

            if (radtxtCapitalCost.Value.HasValue)
                CapitalCost = (decimal)radtxtCapitalCost.Value.Value;

            if (radtxtOtherCost.Value.HasValue)
                OtherCost = (decimal)radtxtOtherCost.Value.Value;

            ContractBll bll = new ContractBll();
            CtrtContractEntity entity = bll.Get<CtrtContractEntity>(new Guid(Request.QueryString["ContractID"])); 
            entity.CapitalCost = CapitalCost;
            entity.OtherCost = OtherCost;
            bll.Update(entity);
          
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            ReLoadData();
        }
    }

}
